#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int MAX_TIME=100;
int Frame_time=100;
double prob_error=.1;
int error_received_by_node2;
/*------*/
#define TRUE                    1  // Boolean true
#define FALSE                   0  // Boolean false
#define NUM_BITS         10000000  // Number of bits to simulate
#define MAX_BURST               5  // Maximum burst size to count
//#define RAND_MAX                1//
/*------*/
int main()
{
FILE *fp,*fp1;
int time,i,trans_time,index_error_in_rcvd[MAX_TIME];
double z;
char packet[10],packet_in_error[10];



fp=fopen("node_1","w+");
for(time=0;time< (MAX_TIME);time++)
fprintf(fp,"paket12 %d\n",time);
fclose(fp);

//-----packet corruption

fp1=fopen("node_1","r");
fp=fopen("node_1_error","w");

for(i=0;i<(MAX_TIME);i++)
{
fscanf(fp1,"%s %d\n",packet,&trans_time);
printf("Read Packet %s %d\n",packet,trans_time);
 // Pull a uniform RV between 0 and 1
    z = (double) rand()/RAND_MAX;
if (z < prob_error)
{
fprintf(fp,"error %d\n",i);
printf("error in packet %f\n",z);
}else
{fprintf(fp,"packet %d\n",i);
printf("No error %f  %f\n",z,prob_error);
}

}
fclose(fp1);
fclose(fp);

//node 2 now reads them
error_received_by_node2=0;
fp=fopen("node_1_error","r");
	for(i=0;i<(MAX_TIME);i++)
	{
	fscanf(fp,"%s %d\n",packet,&trans_time);
		if(strcmp(packet,"error")==NULL)
		{
		error_received_by_node2++;
      		index_error_in_rcvd[i]=0;                
		}else
		{
             index_error_in_rcvd[i]=1;
		}

	}
printf("Number of Errors %d\n",error_received_by_node2);
fclose(fp);

printf("Packets which needs to be retransmitted again are following");
for(i=0;i<(MAX_TIME);i++)
if(index_error_in_rcvd[i]==0)
printf("%d ",i);


}
